﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Data;

namespace MathematicallySafe.HeadCounter.Converters
{
    class EnumDescriptionConverter: IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var type = value.GetType();
            string result = Enum.GetName(type,value);

            var fieldInfo = type.GetField(result);
            var attributeArray = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributeArray.Length > 0)
            {
                return  ((DescriptionAttribute)attributeArray[0]).Description;
            }

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        #endregion

    }
}
